/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/9/27 8:35 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import {
    reqDetailShop,
    reqShopCategoryList,
    reqUpdateShop,
} from 'api/index';

export default {
    namespace: 'ShopEditDetail',
    state: {
        shopDetail: {},
        pcategoryOption: [],
        shop_sign: [],
        business_license_image: [],
        catering_service_license_image: [],
        brand_sale_protocol_image: [],
    },
    reducers: {
        setShopDetail(state, { payload: shopDetail }) {
            return { ...state, shopDetail };
        },
        setPcategoryOption(state, { payload: pcategoryOption }) {
            return { ...state, pcategoryOption };
        },

        setShopSign(state, { payload: shop_sign }) {
            return { ...state, shop_sign };
        },
        setBusinessLicenseImage(state, { payload: business_license_image }) {
            return { ...state, business_license_image };
        },
        setCateringServiceLicenseImage(state, { payload: catering_service_license_image }) {
            return { ...state, catering_service_license_image };
        },
        setBrandSaleProtocolImage(state, { payload: brand_sale_protocol_image }) {
            return { ...state, brand_sale_protocol_image };
        },
    },
    effects: {
        * getShopDetail({ payload }, { call, put, select }) {
            const result = yield call(reqDetailShop, payload);
            if (result.status === 1) {
                const shopDetail = result.data;
                yield put({
                    type: 'setShopDetail',
                    payload: shopDetail,
                });


                const shop_sign = result.data.shop_sign.split(",").map((item, index) => {
                    return {
                        uid: index,
                        name: item,
                        status: 'done',
                        url: item,
                    };
                });
                yield put({
                    type: 'setShopSign',
                    payload: shop_sign,
                });


                const business_license_image = result.data.business_license_image.split(",").map((item, index) => {
                    return {
                        uid: index,
                        name: item,
                        status: 'done',
                        url: item,
                    };
                });
                yield put({
                    type: 'setBusinessLicenseImage',
                    payload: business_license_image,
                });


                const catering_service_license_image = result.data.catering_service_license_image.split(",").map((item, index) => {
                    return {
                        uid: index,
                        name: item,
                        status: 'done',
                        url: item,
                    };
                });
                yield put({
                    type: 'setCateringServiceLicenseImage',
                    payload: catering_service_license_image,
                });


                const brand_sale_protocol_image = result.data.brand_sale_protocol_image.split(",").map((item, index) => {
                    return {
                        uid: index,
                        name: item,
                        status: 'done',
                        url: item,
                    };
                });
                yield put({
                    type: 'setBrandSaleProtocolImage',
                    payload: brand_sale_protocol_image,
                });

            } else {
                message.error(result.message);
            }
        },
        * getPcategoryOption({ _ }, { call, put, select }) {
            const result = yield call(reqShopCategoryList);
            if (result.status === 1) {
                const pcategoryOption = result.data;
                yield put({
                    type: 'setPcategoryOption',
                    payload: pcategoryOption,
                });
            } else {
                message.error(result.message);
            }
        },
        * updateShop({ payload, callback }, { call, put, select }) {
            const result = yield call(reqUpdateShop, payload);
            if (result.status === 1) {
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if (pathToRegexp('/shop-manage/shop/edit').exec(pathname)) {
                    dispatch({
                        type: 'getShopDetail',
                        payload: {
                            shop_id: query.id,
                        },
                    });
                    dispatch({
                        type: 'getPcategoryOption',
                    });
                }
            });
        },
    },
};
